<!--  -->
<template>
  <div>
    <div class="head">
      <div
        :class="{ item: !item.isSelect, select: item.isSelect }"
        @click="handelClick(index)"
        v-for="(item, index) in status"
      >
        {{ item.value }}
      </div>
    </div>
    <div class="body">
      <div
        class="todo"
        v-for="(todo, index) in showTodo.value"
        :key="index"
        @dblclick="dblclick(index)"
      >
        <span :class="{ toSlash: todo.isDone == 1 }">
          <input
            class="check"
            type="checkbox"
            @change="handelChange(index)"
            :checked="todo.isDone"
            name=""
            :id="index + 10"
          />{{ todo.value }}
          <label :for="index + 10"></label>
        </span>
        <button class="del" @click="handelDel(index)">删除</button>
      </div>
      <div></div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, watchEffect } from "vue";

import { useTodoStore } from "../store/useTodoStore.js";

let status = reactive([
  {
    value: "全部",
    isSelect: true,
  },
  {
    value: "已完成",
    isSelect: false,
  },
  {
    value: "未完成",
    isSelect: false,
  },
]);

const TodoStore = useTodoStore();

let todos = reactive(TodoStore.todos);

const showTodo = reactive([]);

/**
 * 删除
 */

function handelDel(idx) {
  console.log(idx);
  if (idx >= 0) {
    TodoStore.subTodo(idx);
  }
}

/**
 * 选项卡选择三种显示状态
 */
function handelClick(index) {
  for (const item of status) {
    item.isSelect = false;
  }
  status[index].isSelect = true;
  if (status[index].value == "全部" || status[index].value == "已完成") {
    TodoStore.delAllDone(true);
  } else {
    TodoStore.delAllDone(false);
  }
}

/**
 * 双击选中
 */

function dblclick(idx) {
  console.log(idx);
  showTodo.value[idx].isDone = !showTodo.value[idx].isDone;
}

function handelChange(idx) {
  //改变当前点击todo的状态
  console.log(idx);
  showTodo.value[idx].isDone = !showTodo.value[idx].isDone;
}
/**
 * 副作用侦听器负责筛选pinia中的todo数据来显示三种todo
 **/
watchEffect(() => {
  let name = status.filter((item) => item.isSelect === true)[0].value;
  console.log(name);
  if (name === "全部") {
    showTodo.value = todos;
  } else if (name === "已完成") {
    showTodo.value = todos.filter((item) => item.isDone == 1);
  } else if (name === "未完成") {
    showTodo.value = todos.filter((item) => item.isDone !== true);
    console.log(showTodo.value)
  }
});
</script>

<style scoped>
.head {
  width: 300px;
  height: 40px;
  padding-left: 10px;
  display: flex;
  justify-content: space-evenly;
  color: rgb(9, 42, 224);
}
.item {
  color: rgb(6, 210, 236);
  margin-top: 15px;
  transition: all 0.1s linear;
}
.select {
  margin-top: 15px;
  color: rgb(121, 121, 157);
  font-size: larger;
  cursor: pointer;
  border-bottom: 1px solid rgb(121, 121, 157);
}
.item:hover {
  cursor: pointer;
  color: rgb(121, 121, 157);
  font-size: larger;
  border-bottom: 1px solid rgb(121, 121, 157);
}

.todo {
  width: 400px;
  height: 66px;
  user-select: none;
  background-color: #06f05f;
  margin-left: 45px;
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  align-items: center;
  padding-left: 10px;
  border-radius: 15px;
  padding-right: 10px;
  font-size: 20px;
}
.del {
  background-color: #fff;
  width: 80px;
  font-size: 15px;
  user-select: none;
}
span {
  position: relative;
  padding-left: 24px;
}
.check {
  position: absolute;
  visibility: hidden;
  left: 0;
}
.check + label {
  position: absolute;
  left: 0;
  top: 2px;
  width: 20px;
  height: 20px;
  border: 1px solid rgb(180, 132, 249);
  border-radius: 50%;
  background-color: #fff;
  color: rgb(252, 13, 13);
}

.check:checked + label::after {
  content: "";
  position: absolute;
  left: 1px;
  top: 1px;
  width: 16px;
  height: 16px;
  background-color: rgb(180, 132, 249);
  border-radius: 50%;
}
.toSlash {
  text-decoration: line-through;
  color: rgb(148, 144, 144);
}
</style>
<!-- checkbox 复选框变成圆的 -->
